"""
 The Argot marshalling routines for a booklist.
 
 A maximum of 256 books can be stored as only a u8
 is used for the array length.
 
"""

import argot

class BookArrayArgot(object):
    TYPENAME = argot.TypeName('booklist')

    @staticmethod
    def write(out, books, element):
        out.write_named_object( argot.UInt8.name, len(books) )
        #print 'Writing books'
        for book in books:
            out.write_named_object( argot.TypeName('book'), book)

    @staticmethod
    def read(instream, element):
        instream.read_named_object( argot.UInt8.name )
        size = instream.stack.u8
        books = []
        for i in range(0, size):
            instream.read_named_object(argot.TypeName('book'))
            books.append( instream.stack.top.get_object() )

        instream.stack.set_object( element, books )
        
        return argot.TYPE_OK
